package com.correportuvida;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

import com.correportuvida.posicion.GestorDeMapas;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;

public class MapActivity extends FragmentActivity {

	private double lat, longi;
	private String metodoNotificacion; 
	private PolylineOptions lineas;
	private MarkerOptions marker;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		GestorDeMapas gestor = new GestorDeMapas();
		ContenedorDeInstancias contenedor = ContenedorDeInstancias.instance();
		contenedor.getActualizador().subscribir(gestor);
		contenedor.setActividadDeMapa(this);
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.map_layout);
		inicializarMapa();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {		
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	public void finalizar(View view) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Finalizar entrenamiento?");
        builder.setMessage("Seguro que desea salir?");
        builder.setPositiveButton("Finalizar", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int whichButton)
            {
            	ContenedorDeInstancias.instance().getSeguidor().terminarManual();
            	cambiarALayoutFinal();
            }
        });
        builder.setNegativeButton("Cancelar", null);
        builder.create();
        builder.show();		
	}

	private void posicionar(double lat, double lng, GoogleMap mapa) {
		LatLng latLng = new LatLng(lat, lng);
		mapa.clear();
		mapa.addMarker(new MarkerOptions().position(latLng)
				.title("Corredor"));
		
		mapa.moveCamera(CameraUpdateFactory.newLatLng(latLng));
		mapa.setMapType(GoogleMap.MAP_TYPE_NORMAL);
		mapa.animateCamera(CameraUpdateFactory.zoomTo(17));
	}
	
	public void inicializarMapa() {
		GoogleMap maps = ((SupportMapFragment) getSupportFragmentManager()
				.findFragmentById(R.id.mapa)).getMap();
		double latitude = -34.556503;
		double longitude = -58.435056;
		posicionar(latitude, longitude, maps);
		
		//Agrego las lineas de dibujo
		lineas = new PolylineOptions();
		lineas.width(8);
		lineas.color(Color.RED);
		lineas.add(new LatLng(latitude, longitude));
	}
	
	public void onLocationChanged(double latitud, double longitud) {
		lat = latitud;
		longi = longitud;
		
		Handler handler = new Handler(Looper.getMainLooper());
		handler.post(new Runnable() {

			@Override
			public void run() {
				GoogleMap mMap = ((SupportMapFragment) getSupportFragmentManager()
						.findFragmentById(R.id.mapa)).getMap();
				posicionar(lat, longi, mMap);
				mostrarLineas(mMap, lat, longi);
			} 
			
		});
	}

	public void finalizarAutomatico() {
		this.finish();
		cambiarALayoutFinal();		
	}
	
	private void cambiarALayoutFinal(){
		Intent intent = new Intent(this, EndActivity.class);
		startActivity(intent);
	}	
	
	private void mostrarLineas(GoogleMap mapa, double latitud, double longitud) {
		LatLng latLng = new LatLng(latitud, longitud);
		lineas.add(latLng);		
		mapa.addPolyline(lineas);
	}
	
	public void actualizarVelocidad(String metodoNotificacion) {
		this.metodoNotificacion = metodoNotificacion;
		
		Handler handler = new Handler(Looper.getMainLooper());
		handler.post(new Runnable() {

			@Override
			public void run() {
				TextView miLabel = (TextView) findViewById(R.id.notificaciones);
				miLabel.setText(MapActivity.this.metodoNotificacion);
			}
		
		});
	}
	
}
